$(document).ready(function () {

    function inurl_submit(path) {

        var submit = $('#submit');
        var modal = $('#response');

        submit.on('click', () => {

            var url = $('#url').val();
            var create_time = $('#createTime').val();
            var domain = $('#domain').val();
            var email = $('#email').val();
            var status = $('#status').val();
            var success = $('#successTime').val();

            $.post(path,
                {
                    url: url,
                    createTime: create_time,
                    domain: domain,
                    email: email,
                    status: status,
                    successTime: success
                },
                (data) => {
                    var msg = $("#msg");
                    if (data.isok) {
                        msg.text(data.data.msg);
                    } else {
                        msg.text(data.err);
                    }
                    modal.modal();
            })
        });
    }

    function outurl_submit(path) {

        var submit = $('#submit');
        var modal = $('#response');

        submit.on('click', () => {

            var url = $('#url').val();
            var create_time = $('#createTime').val();
            var status = $('#status').val();

            $.post(path,
                {
                    url: url,
                    createTime: create_time,
                    status: status,
                },
                (data) => {
                    var msg = $("#msg");
                    if (data.isok) {
                        msg.text(data.data.msg);
                    } else {
                        msg.text(data.err);
                    }
                    modal.modal();
                })
        });
    }

    function url_inactive(path) {
        var del_btn = $('.inactive');
        del_btn.each((index, element) => {
            var item = $(element);
            item.on('click', (event) => {
                path = path.replace(':id', $(event.target).attr('data-id'));
                $.post(path, {}, (data) => {
                    if (data.isok) {
                        location.reload(true);
                    } else {
                        var modal = $('#response');
                        var msg = $('#msg');
                        msg.text(data.err);
                        modal.modal();
                    }
                })
            })
        })
    }

    var email_body = 'aaa';
    var subject = 'bbb';

    function use_email() {
        var use_btn = $('.use');
        use_btn.each((index, element) => {
            var item = $(element);
            item.on('click', (event) => {
                let path = '/email/:id/use'.replace(':id', $(event.target).attr('data-id'));
                let url = $(event.target).attr('data-url');

                $.post(path, {}, (data) => {
                    if (data.isok) {
                        window.open(url);
                        location.reload(true);
                    } else {
                        var modal = $('#response');
                        var msg = $('#msg');
                        msg.text(data.err);
                        modal.modal();
                    }
                })
            })
        })
        var del_btn = $('.del');
        del_btn.each((index, element) => {
            var item = $(element);
            item.on('click', (event) => {
                let path = '/email/:id/del'.replace(':id', $(event.target).attr('data-id'));
                $.post(path, {}, (data) => {
                    if (data.isok) {
                        location.reload(true);
                    } else {
                        var modal = $('#response');
                        var msg = $('#msg');
                        msg.text(data.err);
                        modal.modal();
                    }
                })
            })
        })
    }

    function setup() {
        const pathname = location.pathname;
        switch (pathname) {
            case '/outurl/add':
                outurl_submit('/outurl/save');
                break;
            case /\/outurl\/(.*?)\/edit/.test(pathname) && pathname:
                outurl_submit('/outurl/' + RegExp.$1 + '/update');
                break;
            case /\/inurl\/(.*?)\/edit/.test(pathname) && pathname:
                inurl_submit('/inurl/' + RegExp.$1 + '/update');
                break;
            case '/inurl/add':
                inurl_submit('/inurl/save');
                break;
            case '/inurl/list':
                url_inactive('/inurl/:id/inactive');
                break;
            case '/outurl/list':
                url_inactive('/outurl/:id/inactive');
                break;
            case /\/email\/list/.test(pathname) && pathname:
                use_email()
                break;
            case /\/email\/show/.test(pathname) && pathname:
                use_email()
                break;
        }
    }

    setup();


})